////<!-- 预订订单流水 -->
import api from '../../../fetch/api'

export default {
  data() {
    var validateRefundServiceFee = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请填写预订服务费退款金额'));
      } else {
        if (value > this.bookingRecordVo.serviceFee) {
          callback(new Error('退款金额不能超过已收金额'));
        }
        callback();
      }
    };
    var validateRefundParkingFee = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请填写预订停车费'));
      } else {
        if (value > this.bookingRecordVo.parkingFee) {
          callback(new Error('退款金额不能超过已收金额'));
        }
        callback();
      }
    };
    return {
      bookingRecordVo: {},
      refundForm: {},
      rules: {
        refundServiceFee: [
          { type: 'number', message: '预订服务费退款金额必须为数值' },
          { validator: validateRefundServiceFee, trigger: 'blur' }
        ],
        refundParkingFee: [
          { type: 'number', message: '预订停车费退款金额必须为数值' },
          { validator: validateRefundParkingFee, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initSearch();
  },
  methods: {
    getList() {
      let params = $.extend({}, this.searchParams, this.otherParams);
      this.getItemList(params);
    },
    //获取列表
    getItemList(aparams) {
      let self = this;
      let setData = {};
      let params = {
        page: this.pageNumber,
        pageSize: this.pageSize
      };

      if (self.setData && self.ones) {
        self.setData.searchList.forEach(element => {
          if (element.name == "query_time" && element.value != '') {
            setData["createTimeFrom"] = element.value.split(" - ")[0];
            setData["createTimeTo"] = element.value.split(" - ")[1];
          } else if ("query_in_time" == element.name && element.value != '') {
            setData["enterTimeFrom"] = element.value.split(" - ")[0];
            setData["enterTimeTo"] = element.value.split(" - ")[1];
          } else {
            setData[element.name] = element.value;
          }
        });
      }
      params = $.extend(setData, params, aparams);
      self.ones = null;
      api.fetchGet('park/booking/record/list.do', params)
        .then(res => {
          let data = res.data;
          self.tbodysInfo = data.rows;
          self.total = data.total;
        }, err => { });
    },
    initSearch() {
      if (this.setData) {
        this.searchList.forEach(element => {
          this.setData.searchList.forEach(elementPar => {
            if (element.name == elementPar.name) {
              if (element.name == "query_time" || element.name == "query_in_time") {
                if (elementPar.value != '') {
                  let today = [
                    new Date(moment().format(elementPar.value.split(" - ")[0])),
                    new Date(moment().format(elementPar.value.split(" - ")[1]))
                  ];
                  console.log('today', today)
                  element.value = today;
                } else {
                  element.value = '';
                }
              } else {
                element.value = elementPar.value
              }
            }
          });
        });
      }
    },
    exportData() {
      let self = this;
      let theadsArr = []
      for(let i = 0; i<self.theadsInfo.length; i++){
        if(self.theadsInfo[i].checked){
          theadsArr.push(self.theadsInfo[i].prop)
        }
      }
      let params = {
        meterList: theadsArr.join(',')
      };
      params = $.extend({}, params, this.searchParams, this.otherParams);
      self.showDialog("确认导出该报表?", '', function () {
        self.toExportAsync('park/booking/record/export.do', params);
      });
    },
    refundBookingRecord(id) {
      let self = this;
      api.fetchGet('park/booking/record/getById.do', { id: id })
        .then(res => {
          if (res.status == 1) {
            var bookingRecordVo = res.data;
            self.bookingRecordVo = bookingRecordVo;
            self.refundForm = {
              id: bookingRecordVo.id,
              refundServiceFee: '',
              refundParkingFee: ''
            };

            self.toOpenModal('refundModal', 1);
          } else {
            self.showToast(res.message, 2);
          }
        }, err => { });
    },
    confirmRefundForm() {
      this.toOpenModal("delConfirmTipModal");
    },
    submitRefundForm() {
      let self = this;
      self.closed('delConfirmTipModal');

      this.$refs['refundForm'].validate((valid) => {
        if (valid) { // 通过验证
          api.fetchPost('park/booking/record/refund.do', self.refundForm)
            .then(res => {
              if (res.status == 1) {
                self.showToast('退款成功', 1);
                self.getList();
                self.closed('refundModal');
              } else {
                self.showToast(res.message, 2);
              }
            }, err => { });
        } else {
          return false;
        }
      });
    },
    copyLink(obj) {
      let self=this;
      let ifx = 2;//标识aomp开票
      let openInvoiceUrl = obj.openUrlInvoice;
      let billCode = obj.billCode;
      let parkCode = obj.parkCode;
      let link = openInvoiceUrl + "?if=" + ifx + "&pc=" + parkCode + "&bc=" + billCode;
      //console.log(link);
      var aux = document.createElement("input");
      var content = link;
      aux.setAttribute("value", content);
      document.body.appendChild(aux);
      aux.select();
      document.execCommand("copy");
      document.body.removeChild(aux);
      self.showToast("复制链接成功", 1, 1500);
    }
  }
}
